<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>编程题</title>
</head>
<body>
    <div id="question1">1.
        <button type="button" onclick="question1(this)">分数序列之和</button>:  <span></span>
    </div>
    <div id="question2">2.
        <button type="button">每个月兔子总数为</button>: <span></span>
    </div>
    <div id="question3">3.
        <button type="button" onclick="question3()">猴子第一天摘得桃子数是</button>: <span></span>
    </div>
    <div id="question4">4.
        请输入两个数,交换他们的位置,例:(5,8)<input type="text" onchange="question4()"> <span></span>
    </div>
    <div id="question5">5.
        请输入一个数判断它是不是一个回文数<input type="text" onchange="question5()"> <span></span>
    </div>
    <div id="question6">6.
        请输入奖金: <input type="text" onchange="question6()">万  
        提成金额为: <span></span>
    </div>
    <div id="question7">7.
        <button type="button" onclick="question7()">1-20阶乘的和是</button>: <span></span>
    </div>
    <div id="question8">8.
        <button type="button" onclick="question8()">所有的水仙花数是</button>: <span></span>
    </div>
    <div id="question9">9.
        请输入一个不多于5位的正整数(0-99999)<input type="text" onchange="question9()">: <span></span>
    </div>
    <div id="question10">10.
        <button type="button">每个类创建的个数</button>: <span></span>
    </div>
    <div id="question11">11.
        请输入一个日期(yyyy-mm-dd) <input type="text" onchange="question11()">: <span></span>
    </div>
    <div id="question12">12.
        请输入10个数,例:1,2,3,4,5,6,7,8,9,10 <input type="text" onchange="question12()">: <span></span>
    </div>
    <div id="question13">13.
        请输入星期的首字母,若有重复请继续输入后续字母<input type="text" onchange="question13()">: <span></span>
    </div>
    <div id="question14">14. 
        创建一个点,包括它的X,Y坐标,例:(3,4)<input type="text" onchange="question14()"> : <span></span>
    </div>
    <div id="question15">15.
        <button type="button" onclick="question15()">甲队和乙队的比赛名单是</button>: <span></span>
    </div>
    <div id="question16">16.
        请输入一个数字 <input type="text" onchange="question16()">: <span></span>
    </div>
    <div id="question17">17.
        请输入一个1-100的数字,判断它是不是一个质数<input type="text" onchange="question17()">: <span></span>
    </div>
    <div id="question18">18.
        <button type="button" onclick="question18()">随机读取7个数(1-50)</button>: <span></span>
    </div>
    <div id="question19">19.
        请输入矩形长宽,例:6,8<input type="text" onchange="question19()">它的周长和面积是: <span></span>
    </div>
    <div id="question20">20.
        请输入三个整数,将对其进行排序,例:2,8,5=>2,5,8 <input type="text" onchange="question20()">: <span></span>
    </div>
    <div id="question21">21.
        请输入一个整数,大于7位数,获取它从右端开始4-7位<input type="text" onchange="question21()">: <span></span>
    </div>
    <div id="question22">22.
        输入一个数组(每个元素用逗号分开 例:1,2,3),输出结果为:最大的与第一个元素交换,最小的与最后一个元素交换 <input type="text" onchange="question22()">: <span></span>
    </div>
    <div id="question23">23.
        输入一个数组,逆序输出 <input type="text" onchange="question23()">: <span></span>
    </div>
    <div id="question24">24.
        随便输些什么,会统计字母,数字,空格以及其他字符的个数<input type="text" onchange="question24()"> <span></span>
    </div>
    <div id="question25">25.
        输入一个全是数字的数组,获得他们的和与平均值 <input type="text" onchange="question25()">: <span></span>
    </div>
    <div id="question26">26.
        <button type="button" onclick="question26()">9*9口诀表</button>: <table></table>
    </div>
    <div id="question27">27.
        <button type="button">编写接口</button>
    </div>
    <div id="question28">28.
        <button type="button" onclick="question28()">第五个人的岁数是:</button>: <span></span>
    </div>
    <div id="question29">29.
        输入一个数和相加他几次的次数,例:输入2,5=>2+22+222+2222+22222 <input type="text" onchange="question29()">: <span></span>
    </div>
    <div id="question30">30.
        <button type="button" onclick="question30()">第10次反弹高度为</button>: <span></span>
    </div>
    <div id="question31">31.
        <button type="button" onclick="question31()">该数为</button>: <span></span>
    </div>
    <div id="question32">32.
        <input type="text"> <button type="button" onclick="question32()">1,11,111,1111</button>: <span></span>
    </div>
    <div id="question33">33.
        输入两段字符串,例:abc,def=>abcdef<input type="text" onchange="question33()">: <span></span>
    </div>
    <div id="question34">34.
        请输入一个电话号码<input type="text" onchange="question34()">: <span></span>
    </div>
    <div id="question35">35.
        <button type="button" onclick="question35()">5的阶乘</button>: <span></span>
    </div>
    <script src="./prime.js"></script>
    <script>
        function question1(){
            let q1 = document.getElementById("question1");
            let button = q1.children[0],span = q1.children[1];
            let arr = [2,3],arr1 = [1,2],sumNum = 2/1+3/2;
            for (let i=2;i<20;i++){
                arr.push(arr[i-1]+arr[i-2]);
                arr1.push(arr1[i-1]+arr1[i-2]);
                sumNum += (arr[i-1]+arr[i-2])/(arr1[i-1]+arr1[i-2]);
            }
            span.innerHTML = sumNum;
            //console.log(arr,arr1,sumNum)
        }

        function question2(n){
            // let rab = 2,bigRab = 0;
            // let mon = 1;
            // var canPush = function(month) {
            //     for(let i=0;i<month;i++){

            //     }
            // }
            // for(let i=1;i<n+1;i++){
            //     if(n % 3 == 0 && n/3 > 0){
            //         let nums = n/3;
            //     }else if(n>3){
            //         rab += 2
            //     }
            // }
        }

        function question3(){
            let q3 = document.getElementById("question3");
            let button = q3.children[0],span = q3.children[1];
            let nums = 1;
            for(let i=0;i<10;i++){
                nums = (nums + 1)*2;
            }
            span.innerHTML = nums;
        }

        function question4(){
            let q4 = document.getElementById("question4");
            let input = q4.children[0],span = q4.children[1];
            let val = input.value.split(",");
            span.innerHTML = val.reverse();
        }

        function question5(){
            let q5 = document.getElementById("question5");
            let input = q5.children[0],span = q5.children[1];
            let val = input.value.split("");
            //debugger
            if(val.join() !== val.reverse().join()){
                span.innerHTML = "这不是一个回文数";
            }else{
                span.innerHTML = "这是一个回文数" + val.reverse().join("");
            }
        }

        function question6(){
            let q6 = document.getElementById("question6");
            let input = q6.children[0],span = q6.children[1];
            let val = +input.value,price = 0;
            switch(true){
                case val < 10 || val == 10:
                price = val*0.1;
                break;
                case val > 10 && (val < 20 || val == 20):
                price = (val-10)*0.075 + 10*0.1;
                break;
                case val > 20 && (val < 40 || val == 40):
                price = (val-20)*0.05 + 10*0.075 + 10*0.1;
                break;
                case val > 40 && (val < 60 || val == 60):
                price = (val-40)*0.03 + 20*0.05 + 10*0.075 + 10*0.1;
                break;
                case val > 60 && (val < 100 || val == 100):
                price = (val - 60)*0.015 + 20*0.03 + 20*0.05 + 10*0.075 + 10*0.1;
                break;
                case val > 100:
                price = (val - 100)*0.01 + 40*0.015 + 20 * 0.03 + 20 * 0.05 + 10 * 0.075 + 10 * 0.1;
                break;
            }
            span.innerHTML = price;
        }
        
        function question7(){
            let q7 = document.getElementById("question7");
            let button = q7.children[0],span = q7.children[1];
            let nums = 1;
            var factorial = function (num){
                let res = 1
                for(let i=1;i<num+1;i++){
                    res = res * i;
                }
                return res;
            }
            for (let i=2;i<21;i++){
                nums += factorial(i);
            }
            span.innerHTML = nums;
        }

        function question8(){
            let q8 = document.getElementById("question8");
            let button = q8.children[0],span = q8.children[1];
            let arr = [];
            for(let i=100;i<1000;i++){
                let g = i % 10,s = Math.floor((i % 100) / 10),b = Math.floor(i/100);
                if(Math.pow(g,3)+Math.pow(s,3)+Math.pow(b,3) === i){
                    arr.push(i);
                }
            }
            span.innerHTML = arr.join(",");
        }

        function question9(){
            let q9 = document.getElementById("question9");
            let input = q9.children[0],span = q9.children[1];
            let val = input.value,len = 0;
            switch(val.length){
                case 1: len = 1;break;
                case 2: len = 2;break;
                case 3: len = 3;break;
                case 4: len = 4;break;
                case 5: len = 5;break;
                default: span.innerHTML = '你输入的什么东西?'; throw new Error("你输入的什么东西?");

            }
            span.innerHTML = '它是'+len+'位数,逆序数字为:'+val.split("").reverse().join("");
        }

        function question10(){

        }

        function question11(){
            let q11 = document.getElementById("question11");
            let input = q11.children[0],span = q11.children[1];
            let val = input.value.split("-");
            let y = +val[0],m = +val[1],d = +val[2],res = 0;
            var getM = function(m,r){
                let days = 0;
                switch(m){
                    case 1: days = 31;break;
                    case 2: r?days = 29:days = 28;break;
                    case 3: days = 31;break;
                    case 4: days = 30;break;
                    case 5: days = 31;break;
                    case 6: days = 30;break;
                    case 7: days = 31;break;
                    case 8: days = 31;break;
                    case 9: days = 30;break;
                    case 10: days = 31;break;
                    case 11: days = 30;break;
                    case 12: days = 31;break;
                }
                return days;
            }
            if(y%4 === 0 && y%100 !== 0){//闰年
                if(m > 1){
                    for(let i=1;i<m;i++){
                        res += getM(i,1); 
                    }
                    res += d;
                }else{
                    res = d;
                }
            }else{
                if(m > 1){
                    for(let i=1;i<m;i++){
                        res += getM(i);
                    }
                    res += d;
                }else{
                    res = d;
                }
            }
            span.innerHTML = '这天是这一年的第'+res+'天.';
        }

        function question12(){
            let q12 = document.getElementById("question12");
            let input = q12.children[0], span = q12.children[1];
            let val = input.value.split(",");
            span.innerHTML = val.sort(function(a,b){return a-b;});
        }

        function question13() {
            let q13 = document.getElementById("question13");
            let input = q13.children[0], span = q13.children[1];
            let val = input.value,str = '';
            let date = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
            for(let i=0;i<date.length;i++){
                if(val.length>1){
                    if(date[i].indexOf(val) !== -1 && val !== ''){
                        str += date[i] + "   ";
                    }
                }else if(val.length == 1){
                    if(date[i].charAt(0) === val){
                        str += date[i] + "   ";
                    }
                }
                
            }
            span.innerHTML = str;
        }

        function question14(){
            let q14 = document.getElementById("question14");
            let input = q14.children[0],span = q14.children[1],button = q14.children[2];
            let val = input.value;
            span.innerHTML = val;
        }

        function question15(){
            let q15 = document.getElementById("question15");
            let button = q15.children[0],span = q15.children[1];
            let teamA = ["a","b","c"],teamB = ["x","y","z"];
            let res = [],noRes = ["ax","cx","cz"];
            for(let i=0;i<teamA.length;i++){
                for(let j=0;j<teamB.length;j++){
                    res.push(teamA[i]+teamB[j]);
                }
            }
            for(let i=0;i<noRes.length;i++){
                if(res.indexOf(noRes[i]) !== -1){
                    res.splice(res.indexOf(noRes[i]),1);
                }
            }
            span.innerHTML = res.join(",");
        }

       
        function $_(id) {
            return document.getElementById(id);
        }
        var in_cal = false;
        function question16() {//若想检测此题目需在服务器中运行此页面
            if (in_cal) {
                alert('分解过程中...请勿打搅~');
                return;
            }
            in_cal = true;
            $_('question16').children[1].innerHTML = "分解过程中，请稍候...";

            var num = $_("question16").children[0].value;
            var rst = '';
            if (!isNaN(parseInt(num))) {
                if(typeof(Worker)!=="undefined") {
                    // var worker_blob = new Blob([document.getElementById("cal_worker").textContent]);  
                    var worker = new Worker("./test.js");
                    worker.postMessage([num, PrimesTable, []]);  
                    worker.onmessage = function(event) {  
                        in_cal = false;
                        rst = event.data;
                        if (rst == 'out_of_cal') {
                            rst = "不是我计算不出来，是计算量太大，怕你的电脑算挂了！"
                        }
                        else {
                            rst = "分解质因数结果为：" + rst.join("*")
                        }
                        in_cal = false;
                        $_('question16').children[1].innerHTML = rst;
                        return ;
                    };  
                }
                else {
                    //不支持worker
                    rst = "你的浏览器版本太低级，无法进行Worker计算，请更新到最新版本浏览器！"
                    in_cal = false;
                    $_('question16').children[1].innerHTML = rst;
                }
            }
            else {
                rst = "请输入一个数字，只有数字才能进行质因数分解！";
                in_cal = false;
                $_('question16').children[1].innerHTML = rst;
            }
        }

        function question17() {
            let q17 = document.getElementById("question17");
            let input = q17.children[0],span = q17.children[1],button = q17.children[2];
            let val = +input.value;
            var checkNum = function(num){
                let temp = [];
                for(let i=2;i<=Math.sqrt(num);i++){
                    if(num%i === 0){
                        temp.push(num);
                        break;
                    }
                }
                console.log(temp);
                return temp;
            }
            if(checkNum(val).length>0 || val<3){
                span.innerHTML = '这不是一个素数!';
            }else{
                span.innerHTML = '这是一个素数!';
            };
        }
        
        function question18(){
            let q18 = document.getElementById("question18");
            let button = q18.children[0],span = q18.children[1];
            var getNum = function(){
                return Math.floor(Math.abs(Math.random()*100-50)) + 1;
            }
            let num = [getNum(),getNum(),getNum(),getNum(),getNum(),getNum(),getNum()];
            span.innerHTML = num.join(",");
        }

        function question19(){
            let q19 = document.getElementById("question19");
            let input = q19.children[0],span = q19.children[1];
            let width = +input.value.split(",")[0],height = +input.value.split(",")[1];
            span.innerHTML = "周长是:" + 2*(width + height) + " 面积是:" + width*height;
        }

        function question20(){
            let q20 = document.getElementById("question20");
            let input = q20.children[0],span = q20.children[1];
            let numArr = [+input.value.split(",")[0],+input.value.split(",")[1],+input.value.split(",")[2]];
            span.innerHTML = numArr.sort(function(a,b){return a-b;});
        }

        function question21(){
            let q21 = document.getElementById("question21");
            let input = q21.children[0],span = q21.children[1],button = q21.children[2];
            let val = input.value;
            if(typeof +val !== "number" || isNaN(+val) || val.length<7){
                span.innerHTML = "请输入一个7位以上的整数!";
                return false;
            }
            span.innerHTML = val.substring(val.length-7,val.length-3) + " 反转 " + val.substring(val.length-7,val.length-3).split("").reverse().join("");
        }

        function question22(){
            let q22 = document.getElementById("question22");
            let input = q22.children[0],span = q22.children[1];
            let val = input.value.split(",");
            let min = +val[0],max = +val[0];
            for(let i=0;i<val.length;i++){
                let n = +val[i];
                if(n>max){
                    max = n;
                }
                if(n<min){
                    min = n;
                }
            }
            let first = val.shift(),last = val.pop();
            val.splice(val.indexOf(min+""),1,last);
            val.splice(val.indexOf(max+""),1,first);
            val.push(min);val.unshift(max);
            span.innerHTML = val;
        }

        function question23() {
            let q23 = document.getElementById("question23");
            let input = q23.children[0],span = q23.children[1];
            let val = input.value.split(",");
            span.innerHTML = val.reverse().join(",");
        }

        function question24() {
            let q24 = document.getElementById("question24");
            let input = q24.children[0],span = q24.children[1];
            let val = input.value;
            let a = val.match(/[a-z]|[A-Z]/g).length,num = val.match(/[0-9]/g).length,space = val.match(/\s/g).length;
            let other = val.length - a - num - space;
            span.innerHTML = "字母:"+a+" 数字:"+num+" 空格:"+space+" 其他:"+other;
        }

        function question25() {
            let q25 = document.getElementById("question25");
            let input = q25.children[0],span = q25.children[1];
            let val = input.value.split(",");
            let len = val.length,add = 0,avg = 0;
            for(let i=0;i<len;i++){
                add += +val[i];
            }
            avg = add/len;
            span.innerHTML = "和:"+add+" 平均值:"+avg;
        }

        function question26() {
            let q26 = document.getElementById("question26");
            let button = q26.children[0],table = q26.children[1];
            let tableStr = "<thead></thead><tbody>";
            for(let i=1;i<10;i++){
                tableStr += '<tr>';
                    for(let j=1;j<i+1;j++){
                        tableStr += '<td>'+ i + "*" + j + "=" + i*j + '</td>';
                    }
                tableStr += '</tr>'
            }
            tableStr += '</tbody>'
            table.innerHTML = tableStr;
        }

        function question27() {

        }

        function question28() {
            let q28 = document.getElementById("question28");
            let button = q28.children[0],span = q28.children[1];
            let first = 10;
            span.innerHTML = first + 2 + 2 + 2 + 2;
        }

        function question29() {
            let q29 = document.getElementById("question29");
            let input = q29.children[0],span = q29.children[1];
            let val = input.value.split(",");
            let str = val[0],num = 0;
            for(let i=1;i<val[1];i++){
                str += "+";
                for(let j=0;j<i + 1;j++){
                    str += val[0];
                }
            }
            num = eval(str);
            span.innerHTML = str + "=" + num;
        }

        function question30() {
            let q30 = document.getElementById("question30");
            let button = q30.children[0],span = q30.children[1];
            let height = 100;
            for(let i=0;i<11;i++){
                height /= 2;
            }
            span.innerHTML = height;
        }

        function question31() {
            let q31 = document.getElementById("question31");
            let button = q31.children[0],span = q31.children[1];
            for(let i=0;i<999;i++){
                let a = Math.sqrt(i+100);
                let b = Math.sqrt(i+100+168);
                if((a % 1 === 0 ) && (b % 1 === 0)){
                    span.innerHTML = i;
                    break;
                }
            }
        }

        function question32() {
            let q32 = document.getElementById("question32");
            let input = q32.children[0],button = q32.children[1],span = q32.children[2];
            let val = +input.value,arr = [1,11,111,1111];
            arr.push(val);
            arr.sort(function(a,b){return a-b;});
            span.innerHTML = arr.join(",");
        }

        function question33() {
            let q33 = document.getElementById("question33");
            let input = q33.children[0],span = q33.children[1];
            let val = input.value.split(",");
            span.innerHTML = val[0] + val[1];
        }

        function question34() {
            let q34 = document.getElementById("question34");
            let input = q34.children[0],span = q34.children[1];
            let val = input.value.split("");
            let temp = val.map(function(a){
                return (+a+5) % 10;
            });
            console.log(temp);
            // Array.prototype.change = function (a,b){
            //     let c = a;
            //     a = b;
            //     b = c;
            // }
            // temp.change(temp[0],temp[3]);
            // temp.change(temp[1],temp[2]);
            let t = temp[0];
            temp[0] = temp[3];
            temp[3] = t;
            let tt = temp[1];
            temp[1] = temp[2];
            temp[2] = tt;
            // var change = function(a,b){
            //     let c = a;
            //     a = b;
            //     b = c;
            // }
            // change(temp[0],temp[3]);
            // change(temp[1],temp[2]);
            let str = temp.join("");
            span.innerHTML = str;
        }

        function question35() {
            let q35 = document.getElementById("question35");
            let button = q35.children[0],span = q35.children[1];
            let res = 1;
            for(let i=5;i>0;i--){
                res *= i;
            }
            span.innerHTML = res;
        }
    </script>
</body>
</html>